package com.steak.system.service.artisan.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.steak.system.common.exception.BusinessException;
import com.steak.system.common.exception.EmBusinessError;
import com.steak.system.mapper.ApplyDOMapper;
import com.steak.system.mapper.CollegeDOMapper;
import com.steak.system.mapper.RecruitDOMapper;
import com.steak.system.mapper.UserDOMapper;
import com.steak.system.pojo.dataobject.ApplyDO;
import com.steak.system.pojo.dataobject.CollegeDO;
import com.steak.system.pojo.dataobject.RecruitDO;
import com.steak.system.pojo.dataobject.UserDO;
import com.steak.system.pojo.dto.ApplyDTO;
import com.steak.system.pojo.dto.RecruitDTO;
import com.steak.system.pojo.dto.UserDTO;
import com.steak.system.service.artisan.ArtisanService;
import com.steak.system.util.ConvertUtils;
import com.steak.system.util.PageUtil;
import com.steak.system.util.TokenUtil;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

@Service
public class ArtisanServiceImpl implements ArtisanService {
    @Autowired
    private RecruitDOMapper recruitDOMapper;
    @Autowired
    private CollegeDOMapper collegeDOMapper;
    @Autowired
    private UserDOMapper userDOMapper;
    @Autowired
    private ApplyDOMapper applyDOMapper;

    @Transactional
    @Override
    public void releasePlan(String title, int num, String start, String end, int department, String requirements, String userId) {
        RecruitDO recruitDO = new RecruitDO();
        recruitDO.setStatus(1);
        recruitDO.setRecruitName(title);
        recruitDO.setRecruitPeopleNum(num);
        recruitDO.setRecruitStart(ConvertUtils.stringToDate(start));
        recruitDO.setRecruitEnd(ConvertUtils.stringToDate(end));
        recruitDO.setApplyDepatment(department);
        recruitDO.setRecruitRequirements(requirements);
        recruitDO.setReleaseTime(ConvertUtils.formatDate(new Date()));
        recruitDO.setReleasePeopleId(userId);
        recruitDOMapper.insertSelective(recruitDO);
    }

    @Override
    public List<CollegeDO> getAllDepartment() {
        return collegeDOMapper.list();
    }

    @Transactional
    @Override
    public List<RecruitDTO> getAllRecruit(int page) throws BusinessException {
        PageHelper.startPage(1, PageUtil.getPageSize());
        List<RecruitDTO> list = recruitDOMapper.getAllRecruitDTO(new RecruitDTO());
        List<RecruitDTO> listResult = new LinkedList<>();
        for (int i = 0 ; i < list.size() ; i++){
            RecruitDTO recruitDTO = new RecruitDTO();
            RecruitDTO recruitDTOs = (RecruitDTO) ConvertUtils.DtoToDto(list.get(i),recruitDTO);
            if (list.get(i).getStatus().equals("1")){
                recruitDTOs.setStatus("待审核");
            }else if (list.get(i).getStatus().equals("2")){
                recruitDTOs.setStatus("通过");
            }else {
                recruitDTOs.setStatus("不通过");
            }
            listResult.add(recruitDTOs);
        }
        return listResult;
    }

    @Override
    public RecruitDTO getRecruitInfoById(int recruitId) {
        RecruitDO recruitDO = recruitDOMapper.selectByPrimaryKey(recruitId);
        RecruitDTO recruitDTO = (RecruitDTO) ConvertUtils.DoToDto(recruitDO, new RecruitDTO());
        if (recruitDO.getStatus() == 1) {
            recruitDTO.setStatus("待审核");
        } else if (recruitDO.getStatus() == 2) {
            recruitDTO.setStatus("已通过");
        } else {
            recruitDTO.setStatus("不通过");
        }
        UserDO userDO = userDOMapper.selectByUserId(recruitDO.getReleasePeopleId());
        CollegeDO collegeDO = collegeDOMapper.selectByPrimaryKey(recruitDO.getApplyDepatment());
        recruitDTO.setUserId(userDO.getUserId());
        recruitDTO.setUserName(userDO.getUserName());
        recruitDTO.setReleaseTime(ConvertUtils.dateToString(recruitDO.getReleaseTime()));
        recruitDTO.setRecruitStart(ConvertUtils.dateToString(recruitDO.getRecruitStart()));
        recruitDTO.setRecruitEnd(ConvertUtils.dateToString(recruitDO.getRecruitEnd()));
        recruitDTO.setApplyDepatment(collegeDO.getCollegeName());
        return recruitDTO;
    }

    @Override
    public void checkOK(int recruitId, String title, int num, String start, String end, String requirements) {
        RecruitDO recruitDO = new RecruitDO();
        recruitDO.setRecruitId(recruitId);
        recruitDO.setRecruitName(title);
        recruitDO.setRecruitPeopleNum(num);
        recruitDO.setRecruitStart(ConvertUtils.stringToDate(start));
        recruitDO.setRecruitEnd(ConvertUtils.stringToDate(end));
        recruitDO.setRecruitRequirements(requirements);
        recruitDO.setStatus(2);
        recruitDOMapper.updateByPrimaryKeySelective(recruitDO);
    }

    @Override
    public void checkNO(int recruitId) {
        RecruitDO recruitDO = new RecruitDO();
        recruitDO.setStatus(3);
        recruitDO.setRecruitId(recruitId);
        recruitDOMapper.updateByPrimaryKeySelective(recruitDO);
    }

    /**
     *查询我发布的招聘信息
     * @param userId
     * @return
     */
    @Override
    public List<RecruitDTO> getReleaseRecruitByUserId(String userId) {
        List<RecruitDTO> list = recruitDOMapper.getRecruitDTO(userId);
        List<RecruitDTO> listResult = new LinkedList<>();
        for (int i = 0 ; i < list.size() ; i++){
            RecruitDTO recruitDTO = new RecruitDTO();
            RecruitDTO recruitDTOs = (RecruitDTO) ConvertUtils.DtoToDto(list.get(i),recruitDTO);
            if (list.get(i).getStatus().equals("1")){
                recruitDTOs.setStatus("待审核");
            }else if (list.get(i).getStatus().equals("2")){
                recruitDTOs.setStatus("通过");
            }else {
                recruitDTOs.setStatus("不通过");
            }
            listResult.add(recruitDTOs);
        }
        return listResult;
    }

    /**
     * 查看我发布的招聘信息有哪些学生报名
     * @param userId
     * @return
     */
    @Override
    public List<ApplyDTO> getApplyStudentByRecruitId(String userId){
        List<ApplyDTO> list = applyDOMapper.getApplyDTO(userId);
        List<ApplyDTO> listResult = new LinkedList<>();
        for (int i = 0 ; i < list.size() ; i++){
            ApplyDTO applyDTO = new ApplyDTO();
            ApplyDTO applyDTOs = (ApplyDTO) ConvertUtils.DtoToDto(list.get(i),applyDTO);
            if (list.get(i).getStatus().equals("1")){
                applyDTOs.setStatus("待审核");
            }else if (list.get(i).getStatus().equals("2")){
                applyDTOs.setStatus("通过");
            }else {
                applyDTOs.setStatus("不通过");
            }
            if (list.get(i).getSex().equals("1")){
                applyDTOs.setSex("男");
            }else {
                applyDTOs.setSex("女");
            }
            listResult.add(applyDTOs);
        }
        return listResult;
    }

    @Override
    public List<ApplyDTO> alreadyCheckStudentList(String userId) {
        List<ApplyDTO> list = applyDOMapper.alreadyCheckStudentList(userId);
        List<ApplyDTO> listResult = new LinkedList<>();
        for (int i = 0 ; i < list.size() ; i++){
            ApplyDTO applyDTO = new ApplyDTO();
            ApplyDTO applyDTOs = (ApplyDTO) ConvertUtils.DtoToDto(list.get(i),applyDTO);
            if (list.get(i).getStatus().equals("1")){
                applyDTOs.setStatus("待审核");
            }else if (list.get(i).getStatus().equals("2")){
                applyDTOs.setStatus("通过");
            }else {
                applyDTOs.setStatus("不通过");
            }
            if (list.get(i).getSex().equals("1")){
                applyDTOs.setSex("男");
            }else {
                applyDTOs.setSex("女");
            }
            listResult.add(applyDTOs);
        }
        return listResult;
    }
}
